# -*- coding: utf-8 -*-
'''
Created on 2013-5-12

@author: yq
'''
from Common.htmlHlep import Find
from Common.tool import tool
from Cms import config
from Cms.plus import  page,user
from Models import  models
import  hashlib,datetime,random,SysFileController
from django.db import transaction
from AdminMain.ArticleCommon import  ArticleCommon

@page.page_render(user.Admin())
def Template(request):
        '''
                            列表页
        '''
        info=models.Article_Content()
        return Find.View(request,info)

@page.page_render(user.Admin())
def Index(request):
        '''
                            列表页
        '''
        info=models.Article_Content()
        return Find.View(request,info)
    
    
@page.page_render(user.Admin())
def List(request):
        '''
                            列表页
        '''
        state=int(request.POST.get("State",-1))
        templateId=int(request.POST.get("TemplateId",-1))
        articleTypeid=str(request.POST.get("ArticleTypeid",""))
        title=request.POST.get("Title","")
        list=models.Article_Content.objects.all()
        
        if title!="":
            title= list.filter(Title=title)
            
        if templateId!=-1:
            list= list.filter(TemplateId=models.Article_Template.objects.get(id=templateId))
            
        if articleTypeid!="":
            list= list.filter(ArticleTypeid=models.Article_Type.objects.get(ArticleTypeid=articleTypeid))
            
        if state!=-1:
            list= list.filter(State=state)
            
     
        list=list.order_by("-TopNum","-SubmitTime")#倒序加上-号
        
        return Find.View(request,Find.paginator(request,list))

def Edit(request):
        '''
                    修改
        '''
        contentId=request.GET.get("ContentId","")
        info=models.Article_Content.objects.get(ContentId=contentId)
        if info.TemplateId.id==1:
            return Find.Redirect("/AdminMain/Article/EditNews?ContentId="+contentId)
        if info.TemplateId.id==2:
            return Find.Redirect("/AdminMain/ArticleImg/Edit?ContentId="+contentId)
        if info.TemplateId.id==5:
            return Find.Redirect("/AdminMain/Article/EditJob?ContentId="+contentId)
        else:
            return Find.Html("is null")

@page.page_render(user.Admin())
def AddNews(request):
        '''
                            添加
        '''
        info=models.Article_Content()
        info.ContentId=tool.guid()
        return Find.View(request,info)

@page.page_render(user.Admin())
@transaction.commit_manually 
def DoAddNews(request):
        '''
                            保存新闻
        '''
        try:
            
            contentId=request.POST.get("KeyId","")
            if len(contentId)=="":
                return Find.JsonX(True, "没得到ID")
           
            if len(models.Article_Content.objects.filter(ContentId=contentId))!=0:
                return Find.JsonX(True, "Id已经存在")
            
            info=ArticleCommon().Add(request)
            
            
            #保存新闻
            news=models.Article_News(Newid=tool.guid())
            Find.UpdateModel(request,news)
            
            #保存文本
            all_the_text=request.POST.get("content","")
            fileinfo=SysFileController.SetTxt(all_the_text, info.ContentId)
            news.ContentId=info
            news.TxtId=fileinfo.FileId
            fileinfo.IsBind=1
            #更新图片文件
            models.Sys_File.objects.filter(ModelId=info.ContentId).update(IsBind=1)
            news.save()
            info.save()
            fileinfo.save()
            transaction.commit()
        except  Exception ,e:
            print e
            transaction.rollback()
            return Find.JsonX(True, "操作失败")
        return Find.JsonX(False, "操作成功")
        
        
@page.page_render(user.Admin())
def EditNews(request):
        '''
                        编辑新闻
        '''
        contentId=request.GET.get("ContentId","")
        info=models.Article_Content.objects.get(ContentId=contentId)
        news=models.Article_News.objects.get(ContentId=contentId)
        txt=SysFileController.GetTxt(news.TxtId)
        return Find.View(request,{"info":info,"news":news,"txt":txt})

@page.page_render(user.Admin())
@transaction.commit_manually 
def DoEditNews(request):
        '''
                            编辑新闻
        '''
        try:
            info=ArticleCommon().Edit(request)
            
            #保存新闻
            news=models.Article_News.objects.get(ContentId=info)
            Find.UpdateModel(request,news)
            
            #保存文本
            all_the_text=request.POST.get("content","")
            #print all_the_text
            fileinfo=SysFileController.EditTxt(all_the_text, news.TxtId)
            
            #更新图片文件
            models.Sys_File.objects.filter(ModelId=info.ContentId).update(IsBind=1)
            
            
            news.save()
            info.save()
            fileinfo.save()
            transaction.commit()
        except Exception,e:
            transaction.rollback()
            print e
            return Find.JsonX(True, "操作失败")
        return Find.JsonX(False, "操作成功")



'****************job*****************************'
@page.page_render(user.Admin())
def AddJob(request):
        '''
                            添加
        '''
        info=models.Article_Content()
        info.ContentId=tool.guid()
        return Find.View(request,info)


@page.page_render(user.Admin())
@transaction.commit_manually 
def DoAddJob(request):
        '''
                            保存招聘
        '''
        try:
            
            contentId=request.POST.get("KeyId","")
            if len(contentId)=="":
                return Find.JsonX(True, "没得到ID")
           
            if len(models.Article_Content.objects.filter(ContentId=contentId))!=0:
                return Find.JsonX(True, "Id已经存在")
            
            info=ArticleCommon().Add(request,5)
            
            #保存新闻
            news=models.Article_Job(JodId=tool.guid())
            news.ContentId=info
            Find.UpdateModel(request,news)
            
            #更新图片文件
            models.Sys_File.objects.filter(ModelId=info.ContentId).update(IsBind=1)
            news.save()
            info.save()
            transaction.commit()
        except  Exception ,e:
            print e
            transaction.rollback()
            return Find.JsonX(True, "操作失败")
        return Find.JsonX(False, "操作成功")
        
        
@page.page_render(user.Admin())
def EditJob(request):
        '''
                        编辑招聘
        '''
        contentId=request.GET.get("ContentId","")
        info=models.Article_Content.objects.get(ContentId=contentId)
        news=models.Article_Job.objects.get(ContentId=contentId)
        return Find.View(request,{"info":info,"news":news})

@page.page_render(user.Admin())
@transaction.commit_manually 
def DoEditJob(request):
        '''
                            编辑招聘
        '''
        try:
            info=ArticleCommon().Edit(request)
            
            #保存新闻
            news=models.Article_Job.objects.get(ContentId=info)
            Find.UpdateModel(request,news)
            #更新图片文件
            models.Sys_File.objects.filter(ModelId=info.ContentId).update(IsBind=1)
            news.save()
            info.save()
            transaction.commit()
        except Exception,e:
            transaction.rollback()
            print e
            return Find.JsonX(True, "操作失败")
        return Find.JsonX(False, "操作成功")





'****************job*****************************'
@page.page_render(user.Admin())
def AddSoft(request):
        '''
                            添加
        '''
        info=models.Article_Content()
        info.ContentId=tool.guid()
        return Find.View(request,info)


@page.page_render(user.Admin())
@transaction.commit_manually 
def DoAddSoft(request):
        '''
                            保存软件下载
        '''
        try:
            
            contentId=request.POST.get("KeyId","")
            if len(contentId)=="":
                return Find.JsonX(True, "没得到ID")
           
            if len(models.Article_Content.objects.filter(ContentId=contentId))!=0:
                return Find.JsonX(True, "Id已经存在")
            
            info=ArticleCommon().Add(request,6)
            
            #保存新闻
            news=models.Article_Soft(JodId=tool.guid())
            news.ContentId=info
            Find.UpdateModel(request,news)
            
            #更新图片文件
            models.Sys_File.objects.filter(ModelId=info.ContentId).update(IsBind=1)
            news.save()
            info.save()
            transaction.commit()
        except  Exception ,e:
            print e
            transaction.rollback()
            return Find.JsonX(True, "操作失败")
        return Find.JsonX(False, "操作成功")
        
        
@page.page_render(user.Admin())
def EditSoft(request):
        '''
                        编辑软件
        '''
        contentId=request.GET.get("ContentId","")
        info=models.Article_Content.objects.get(ContentId=contentId)
        news=models.Article_Soft.objects.get(ContentId=contentId)
        return Find.View(request,{"info":info,"news":news})

@page.page_render(user.Admin())
@transaction.commit_manually 
def DoEditSoft(request):
        '''
                            编辑软件
        '''
        try:
            info=ArticleCommon().Edit(request)
            
            #保存新闻
            news=models.Article_Soft.objects.get(ContentId=info)
            Find.UpdateModel(request,news)
            #更新图片文件
            models.Sys_File.objects.filter(ModelId=info.ContentId).update(IsBind=1)
            news.save()
            info.save()
            transaction.commit()
        except Exception,e:
            transaction.rollback()
            print e
            return Find.JsonX(True, "操作失败")
        return Find.JsonX(False, "操作成功")









    
    
    
    
    
    
    
    